﻿#!/usr/bin/env python
#coding:utf-8

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

class ProfileBase(type):
    """扩展系统User表的元类"""
    def __new__(cls, name, bases,  attrs):
        module = attrs.pop('__module__')
        parents = [b for b in bases if isinstance(b, ProfileBase)]
        if parents:
            fields = []
            for obj_name, obj in attrs.items():
                if isinstance(obj, models.Field):fields.append(obj_name)
                User.add_to_class(obj_name, obj)
            UserAdmin.fieldsets = list(UserAdmin.fieldsets)
            UserAdmin.fieldsets.append((name, {'fields':fields}))

        return super(ProfileBase, cls).__new__(cls, name, bases, attrs)


class ProfileUser(object):
    __metaclass__ = ProfileBase